<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Moto Marca Modelo
 *
 * @package		cms
 * @author		Lucas Volgarini
 */
class Moto_modelo_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->load->config('parametros');
    }

    /**
     * @access	public
     * @return	array
     */
    function getAllModelosOrderByNome() {
        $this->db->order_by("nome ASC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModelo'));
        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	integer
     * @return	array
     */
    function getAllModelosByMarca($_idMarca) {
        $this->db->select('mo.*, ma.nome as marca');
                
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblMarca') . ' as ma', 'ma.id = mo.marca_id', 'INNER OUTER');
        
        $this->db->where('mo.marca_id', $_idMarca);
        $this->db->order_by("mo.nome ASC");

        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModelo') . ' as mo');

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	integer
     * @return	array
     */
    function getModelo($_id) {
        $this->db->select('m.*, e.nome as estilo, ma.nome as marca');

        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblEstilo') . ' as e', 'e.id = m.estilo_id', 'LEFT OUTER');
        $this->db->join($this->config->item('dbCliente') . $this->config->item('tblMarca') . ' as ma', 'ma.id = m.marca_id', 'INNER OUTER');
        
        $this->db->where('m.id', $_id);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModelo') . ' as m');

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	string
     * @return	array
     */
    function getModeloByNome($_nome, $_marca = null) {
        $this->db->where('nome', $_nome);
        if ($_marca != null){
            $this->db->where('marca_id', $_marca);
        }
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModelo'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	string
     * @param   string
     * @return	array
     */
    function getModeloByNomeCilindrada($_nome, $_cilindrada) {
        $this->db->where('nome', $_nome);
        $this->db->where('cilindrada', $_cilindrada);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModelo'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    function getModeloByNomeCilindradaEstilo($_nome, $_cilindrada, $_estilo) {
        $this->db->where('nome', $_nome);
        $this->db->where('cilindrada', $_cilindrada);
        $this->db->where('estilo_id', $_estilo);
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModelo'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    /**
     * @access	public
     * @param	array
     * @return	void
     */
    function inserir($_dados) {
        $this->db->set($_dados);
        $this->db->insert($this->config->item('dbCliente') . $this->config->item('tblModelo'));
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return NULL;
        }
    }

    /**
     * @access	public
     * @param	integer
     * @param   array
     * @return	boolean
     */
    function atualizar($_id, $_dados) {
        $this->db->where('id', $_id);
        $this->db->set($_dados);
        $this->db->update($this->config->item('dbCliente') . $this->config->item('tblModelo'));
        if ($this->db->affected_rows() > 0) {
            return $_id;
        } else {
            return NULL;
        }
    }

    /**
     * @access	public
     * @return	array
     */
    function getAllModeloOrderById() {
        $this->db->order_by("id DESC");
        $query = $this->db->get($this->config->item('dbCliente') . $this->config->item('tblModelo'));

        if ($query->num_rows() > 0) {
            return $query->result();
        } else {
            return null;
        }
    }

    public function combo($_idMarca) {
        $result = $this->getAllModelosByMarca($_idMarca);

        if ($result != null) {
            $option = array();
            $option[''] = 'Selecionar';
            foreach ($result as $value) {
                $option[$value->id] = $value->nome;
            }
            return $option;
        }
        return null;
    }

}